package com.gnerv.author.controller;


import com.gnerv.author.dto.ArticleDTO;
import com.gnerv.author.entity.Article;
import com.gnerv.author.entity.R;
import com.gnerv.author.service.IArticleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 博客平台-文章表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-07-15
 */
@CrossOrigin
@RestController
@RequestMapping("/blog/article")
public class ArticleController {

    @Autowired
    IArticleService articleService;

    @RequiresPermissions("blog:article")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public R create(@RequestBody ArticleDTO articleDTO){
        articleService.saveArticle(articleDTO);
        return R.back();
    }

    @RequiresPermissions("blog:article")
    @RequestMapping(value = "delete/{bId}", method = RequestMethod.GET)
    public R delete(@PathVariable(value = "bId") String bId) {
        articleService.deleteArticle(bId);
        return R.back();
    }

    @RequiresPermissions("blog:article")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public R update(@RequestBody ArticleDTO articleDTO) {
        articleService.saveArticle(articleDTO);
        return R.back();
    }

    @RequiresPermissions("blog:article")
    @RequestMapping(value = "select/{bId}", method = RequestMethod.GET)
    public R select(@PathVariable(value = "bId") String bId) {
        ArticleDTO article = articleService.selectArticle(bId);
        return R.back().data("article", article);
    }

    @RequiresPermissions("blog:article")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public R list(@RequestBody ArticleDTO articleDTO) {
        List<ArticleDTO> articles = articleService.listArticle(articleDTO, null);
        return R.back().data("articles", articles);
    }

    @RequiresPermissions("blog:article")
    @RequestMapping(value = "list/catalog/{cId}", method = RequestMethod.GET)
    public R listByCiD(@PathVariable(value = "cId") String cId) {
        List<ArticleDTO> articles = articleService.listArticleByCid(cId, null);
        return R.back().data("articles", articles);
    }

    @RequiresPermissions("blog:article")
    @RequestMapping(value = "release/{bId}", method = RequestMethod.GET)
    public R release(@PathVariable(value = "bId") String bId) {
        articleService.releaseArticle(bId);
        return R.back();
    }

    @RequiresPermissions("blog:article")
    @RequestMapping(value = "unRelease/{bId}", method = RequestMethod.GET)
    public R unRelease(@PathVariable(value = "bId") String bId) {
        articleService.unReleaseArticle(bId);
        return R.back();
    }

}
